package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PReward;
import com.mk.cloud.common.domain.PUser;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import com.mk.cloud.common.util.S2;
import com.mk.cloud.pt.service.app.RewardService;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Controller("puser")
public class PUserController extends CookieService {

    @Inject
    private PUser.Mapper pUserMapper;
    @Inject
    private PReward.Mapper pRewardMapper;
    @Inject
    private RewardService rewardService;


    @Description("用户审核列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PUser> pUserList(
            @Description("姓名") String realname,
            @Description("昵称") String nickname,
            @Description("店名") String shopname,
            @Description("品牌区域") String taarea,
            @Description("品牌区域") String taid,
            @Description("方案金额") String planamount,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PUser> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        if(S.eq(me.getIsagent(), GlobalValues.FLAG_1.value())){//如果是代理则
            map.put("userid",me.getUserid());
        }

        map.put("realname",realname);
        map.put("nickname",nickname);
        map.put("shopname",shopname);
        map.put("taarea",taarea);
        map.put("planamount",planamount);
        map.put("taid",taid);
        map.put("coid", me.getCoid());
        query.setParas(map);

        pUserMapper.getSQLManager().pageQuery("pUser.select_puser_page_list", PUser.class, query);
        return query;
    }

    @Description("用户审核列表")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PUser getByID(
            @Description("用户ID") String fuserid

    ) {
        return pUserMapper.selectPUserByid(fuserid);
    }

    @Description("用户审核修改")
    @With(ICLogin.class)
    @PutAction("update.html")
    public int update(
            @Description("用户ID") PUser pUser

    ) {
        pUser.setAudittime(DateUtils.getTimeNow());
        PReward reward = new PReward();
        reward.setFuserid(pUser.getFuserid());
        reward.setCoid(pUser.getCoid());
        //TODO 认为现场签单奖励是头一个奖励,不是头一个就不发放
        long count = pRewardMapper.templateCount(reward);
        if(count<=0){
            rewardService.sendObjectReward(pUser.getFuserid(),pUser.getCoid());
        }
        return pUserMapper.updateTemplateById(pUser);
    }

}
